import java.util.ArrayList;
import java.util.List;

public class Solution524 {
    public String findLongestWord(String s, List<String> dictionary) {
        dictionary.sort((a,b)->{
            if (a.length()>b.length()) return -1;
            if (a.length()<b.length()) return 1;
            return a.compareTo(b);
        });
        char[] chars=s.toCharArray();
        for (String s1 : dictionary) {
            char[] chars1=s1.toCharArray();
            int point=0;
            for (char aChar : chars) {
                if (aChar==chars1[point]){
                    point++;
                    if (point==chars1.length){
                        return s1;
                    }
                }
            }
        }
        return "";
    }

    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        list.add("ale");list.add("apple");list.add("monkey");list.add("plea");
        System.out.println(new Solution524().findLongestWord("abpcplea",list));
    }
}
